package com.bianguo.android.httplibrary;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolManager {

    public static final int CORE_POOL_SIZE = 3;
    public static final int MAX_POOL_SIZE = 10;
    private static volatile ThreadPoolManager instance;
    private LinkedBlockingQueue<Runnable> queues;
    private ThreadPoolExecutor executor;
    private DelayQueue<HttpTask> mDelayQueues = new DelayQueue<>();

    private ThreadPoolManager() {
        //创建一个消息队列
        queues = new LinkedBlockingQueue<>();
        //初始化线程池
        initThreadPoolExecutor();
    }

    private void initThreadPoolExecutor() {
        executor = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, 15, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(4), new RejectedExecutionHandler() {
            @Override
            public void rejectedExecution(Runnable runnable, ThreadPoolExecutor threadPoolExecutor) {
                addTask(runnable);
            }
        });
        executor.execute(coreRunnable);
        executor.execute(delayRunnable);
    }

    public static ThreadPoolManager getInstance() {
        ThreadPoolManager manager = instance;
        if (manager == null) {
            synchronized (ThreadPoolManager.class) {
                if (manager == null) {
                    manager = new ThreadPoolManager();
                    instance = manager;
                }
            }
        }
        return manager;
    }

    public void addTask(Runnable task) {
        if (task != null) {
            try {
                queues.put(task);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void addDelayTask(HttpTask task) {
        if (task != null) {
            task.setDelayTime(3000);
            mDelayQueues.put(task);
        }
    }

    private Runnable coreRunnable = new Runnable() {

        Runnable runn = null;

        @Override
        public void run() {
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    runn = queues.take();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                executor.execute(runn);
            }
        }
    };

    private Runnable delayRunnable = new Runnable() {

        HttpTask ht;

        @Override
        public void run() {
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    ht = mDelayQueues.take();
                    int retryCount = ht.getRetryCount();
                    if (retryCount < 3) {
                        executor.execute(ht);
                        ht.setRetryCount(retryCount + 1);
                    } else {

                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };
}
